import 'package:flutter/material.dart';
import 'package:freemusic/model/Lyric.dart';
import 'package:freemusic/utils/Util.dart';

/*
 * 歌词面板
 */
class LyricPanel extends StatefulWidget {
  final Lyric lyric;
  double height;
  double width;
  EdgeInsets margin = EdgeInsets.all(0.0);
  Duration position;
  int lyricIndex;

  LyricPanel(
    this.lyric, {
    this.height,
    this.width,
    this.margin,
    this.position,
    this.lyricIndex,
  });

  @override
  State<StatefulWidget> createState() {
    return LyricPanelState();
  }
}

class LyricPanelState extends State<LyricPanel> {
  int _index = 0;
  LyricSlice currentSlice;
  LyricSlice nextSlice;

  @override
  void deactivate() {
    super.deactivate();
  }

  initLyricIndex() {
    _index = widget.lyric.slices.indexWhere((slice) {
      return slice.milliseconds.inMilliseconds > widget.position.inMilliseconds;
    });
    if (_index <= 0) {
      return _index = 0;
    } else {
      return --_index;
    }
  }

  @override
  Widget build(BuildContext context) {
    Lyric lyric = widget.lyric;
    Color themeColor = Util.themeColor;
    if (lyric.slices != null && lyric.slices.length > 0) {
      initLyricIndex();
      setState(() {
        currentSlice = lyric.slices[_index];
        nextSlice = lyric.slices[++_index];
      });
      return Container(
        height: widget.height,
        width: widget.width,
        margin: widget.margin,
        child: Center(
          child: Container(
            child: Column(
              children: <Widget>[
                Text(
                  currentSlice != null ? currentSlice.slice : "",
                  style: TextStyle(color: themeColor),
                  overflow: TextOverflow.ellipsis,
                ),
                Text(
                  currentSlice != null ? nextSlice.slice : "",
                  style: TextStyle(color: themeColor),
                  overflow: TextOverflow.ellipsis,
                ),
              ],
            ),
          ),
        ),
      );
    } else {
      return Container(
        height: widget.height,
        width: widget.width,
        margin: widget.margin,
        child: Center(
          child: Text("未找到歌词信息"),
        ),
      );
    }
  }
}
